package com.zxy.employee.service;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.base.BusinessException;
import com.zxy.dept.mapper.DeptMapper;
import com.zxy.employee.dto.EmployeeDto;
import com.zxy.employee.entity.EmployeeEntity;
import com.zxy.employee.entity.EmployeeSchoolEntity;
import com.zxy.employee.entity.ResumeEntity;
import com.zxy.employee.excel.EmployeeData;
import com.zxy.employee.excel.UploadEmployeeListener;
import com.zxy.employee.mapper.EmployeeMapper;
import com.zxy.employee.mapper.EmployeeSchoolMapper;
import com.zxy.employee.mapper.ResumeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Service
public class EmployeeService extends ServiceImpl<EmployeeMapper, EmployeeEntity> {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmployeeSchoolMapper employeeSchoolMapper;

    @Autowired
    private ResumeMapper resumeMapper;

    public Page<EmployeeDto> findEmployeeByNameAndStatus(Integer num, Integer size ,String name,Boolean employeeStatus) {
        Page page = new Page(num,size);

        Page<EmployeeDto> employeeDtoPage = employeeMapper.selectPageEmployeeByNameAndStatus(page, name, employeeStatus);

        return employeeDtoPage;
    }

    public EmployeeDto findEmployeeById(Integer employeeId) {
        EmployeeEntity employeeEntity = employeeMapper.selectById(employeeId);

        EmployeeDto employeeDto = new EmployeeDto();

        BeanUtil.copyProperties(employeeEntity,employeeDto,false);

        employeeDto.setDeptName(deptMapper.selectById(employeeEntity.getDeptId()).getDeptName());

        return employeeDto;
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteEmployeeById(Integer id) {

        employeeMapper.deleteById(id);
        employeeSchoolMapper.delete(new QueryWrapper<EmployeeSchoolEntity>().lambda()
                .eq(EmployeeSchoolEntity::getEmployeeId,id));
        resumeMapper.delete(new QueryWrapper<ResumeEntity>().lambda().eq(ResumeEntity::getEmployeeId,id));
        return 1;
    }

    public EmployeeEntity insertEmployee(EmployeeEntity employeeEntity) {

        boolean phoneExists = employeeMapper.exists(new QueryWrapper<EmployeeEntity>().lambda()
                .eq(EmployeeEntity::getPhone, employeeEntity.getPhone()));
        boolean idCard = employeeMapper.exists(new QueryWrapper<EmployeeEntity>().lambda()
                .eq(EmployeeEntity::getIdCard, employeeEntity.getIdCard()));
        if(phoneExists) {
            throw new BusinessException("手机号被占用");
        }
        if(idCard) {
            throw new BusinessException("身份证号被占用");
        }
        int insert = employeeMapper.insert(employeeEntity);
        return employeeEntity;
    }

    public boolean updateEmployee(EmployeeEntity employeeEntity) {
        boolean result = this.updateById(employeeEntity);
        return result;
    }

    public List<EmployeeEntity> insertEmployeeBatch(List<EmployeeEntity> employees) {
        this.saveBatch(employees);
        return employees;
    }


    public String insertEmployeeFromUploadFile(MultipartFile file) throws IOException {
        UploadEmployeeListener uploadEmployeeListener = new UploadEmployeeListener(this);
        EasyExcel.read(file.getInputStream(), EmployeeData.class, uploadEmployeeListener).sheet().doRead();
        String result = uploadEmployeeListener.getResult();

        String filename = null;

        if(!StringUtils.isBlank(result)) {

            filename = "errlog-"+System.currentTimeMillis()+".txt";
            File errmsg = new File(filename);

            FileWriter fileWriter = new FileWriter(errmsg);

            fileWriter.write(result);

            fileWriter.flush();

            fileWriter.close();

            return filename;
        }
        return result;
    }

}
